using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;

public class Coin : NetworkBehaviour
{
    //处理金币消失
    private NetworkVariable<bool> networkIsActive = new NetworkVariable<bool>(true);
    public override void OnNetworkSpawn()
    {
        networkIsActive.OnValueChanged += (preValue, newValue) =>
        {
            this.gameObject.SetActive(newValue);
        };
        gameObject.SetActive(networkIsActive.Value);
    }
    public void SetActive(bool active)
    {
        if (this.IsServer)
        {
            networkIsActive.Value = active;
        }else if (this.IsClient)
        {
            SetServerRpc(active);
        }
    }
    //金币是由服务器创建的，那么金币的Owner是服务器，这里是客户端发起的，所以会报错，所以要忽略必须拥有者调用，
    //意思就是客户端也可以调用
    [ServerRpc(RequireOwnership = false)]
    public void SetServerRpc(bool active)
    {
        networkIsActive.Value = active;
    }
}
